﻿using IService.Admin;
using Model.Parameter.Admin;

namespace Service.Admin
{
    public class MessageService : Base.BaseService<TMessage>, IMessageService
    {
        public MessageService(IUnitOfWorkService se) : base(se)
        {
        }

        public async Task<RM_ApiResult> AllRead4CurrentAdmin()
        {
            await Status<TAdminMessageRelation>(_CurrentAdmin.Id, (int)Enum_MessageStatus.unread);

            return _ResultHelper.BuildActionSuccess();
        }

        public async Task<RM_ApiResult> DeleteMessage4CurrentAdmin(List<int> Keys)
        {
            await Delete<TAdminMessageRelation>(o => o.AdminId == _CurrentAdmin.Id && Keys.Contains(o.Id));
            return _ResultHelper.BuildActionSuccess();
        }

        public async Task<RM_ApiResult> GetPage4CurrentAdmin(Par_AdminPageBase par)
        {
            var exp = Expressionable.Create<TAdminMessageRelation>()
                .AndIF(par.CategoryId > 0, o => o.Message.MsgType == (Enum_MessageType)par.CategoryId)
                .And(o => o.AdminId == _CurrentAdmin.Id)
                .ToExpression();

            var result = new RM_PageReturn<TAdminMessageRelation>
            {
                Datas = await _DbContext.Queryable<TAdminMessageRelation>().Includes(o => o.Message).Where(exp).OrderBy(o => o.Status).OrderBy(o => o.Message.CreateTime, OrderByType.Desc).ToPageListAsync(par.PageNo, par.PageSize),
                TotalCount = await _DbContext.Queryable<TAdminMessageRelation>().CountAsync(exp),
                PageSize = par.PageSize
            };

            foreach (var item in result.Datas)
            {
                item.Message.MsgTypeDesc = _EnumHelper.GetDescription(item.Message.MsgType);
                item.StatusDesc = _EnumHelper.GetDescription((Enum_MessageStatus)item.Status);
            }
            return _ResultHelper.BuildSuccess(result);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public new async Task<RM_ApiResult> GetItem(int key)
        {
            if (await Any<TAdminMessageRelation>(key))
            {
                var data = await _DbContext.Queryable<TAdminMessageRelation>()
                                           .Includes(o => o.Message, x => x.RecycleReq)
                                           .Includes(o => o.Message, x => x.RecycleSend)
                                           .Includes(o => o.Message, x => x.MsgCtrl)
                                           .Includes(o => o.Message, x => x.Customer)
                                           .FirstAsync(o => o.Id == key);
                if (data != null)
                {
                    if (data.Status == (int)Enum_MessageStatus.unread)
                    {
                        data.Status = (int)Enum_MessageStatus.Read;
                        await _DbContext.Updateable(data).ExecuteCommandAsync();
                    }

                    #region 模糊

                    switch (data.Message.MsgType)
                    {
                        case Enum_MessageType.RecycleReq:

                            if (data.Message.RecycleReq != null)
                            {
                                //todo
                            }

                            data.Message.RecycleSend = null;
                            data.Message.MsgCtrl = null;
                            break;

                        case Enum_MessageType.RecycleSend:

                            data.Message.RecycleReq = null;

                            if (data.Message.RecycleSend != null)
                            {
                                //todo
                            }
                            data.Message.MsgCtrl = null;

                            break;

                        case Enum_MessageType.CustMsg:
                            data.Message.RecycleReq = null;
                            data.Message.RecycleSend = null;
                            if (data.Message.MsgCtrl != null)
                            {
                                //todo
                            }
                            break;

                        default:
                            break;
                    }

                    #endregion 模糊

                    data.Message.MsgTypeDesc = _EnumHelper.GetDescription(data.Message.MsgType);
                    data.StatusDesc = _EnumHelper.GetDescription((Enum_MessageStatus)data.Status);
                }

                return _ResultHelper.BuildSuccess(data);
            }
            else
            {
                return _ResultHelper.BuildError(Enum_SysAttributeType.Misc.NoData);
            }
        }
    }
}